<?php

class Service_sitemap extends MY_Service
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Model_urls');
        $this->load->model('Model_sitemap');
        $this->load->library('common/CommonEnum');
    }

    public function make()
    {
        $sitemapConfig = json_decode($this->getConfigVal(CommonEnum::SITEMAP_CONFIG_ID), true);
        $sql = 'SELECT id FROM urls WHERE `status` = 0 AND `show` = 1';
        $list = $this->Model_urls->query($sql);

        $result = [];
        foreach ($list as $item) {
            $result[] = ['url' => sprintf($sitemapConfig['url'], $item['id'])];
        }

        $sql = 'SELECT url FROM sitemap';
        $list = $this->Model_sitemap->query($sql);
        foreach ($list as $item) {
            $result[] = ['url' => $item['url']];
        }

        $data = array_chunk($result, $sitemapConfig['pageNum']);
        $page = count($data);
        for ($i = 1; $i <= $page; $i++) {
            $this->makeSiteMap($data[$i - 1], APPPATH . sprintf($sitemapConfig['filePath'], $i));
        }

        $this->makeSiteMapIndex($sitemapConfig['siteMapUrl'], $page, APPPATH . sprintf($sitemapConfig['filePath'], ''));
    }

    public function make404()
    {
        $sitemapConfig = json_decode($this->getConfigVal(CommonEnum::SITEMAP_CONFIG_ID), true);
        $i = 1;
        do {
            $cond = ['status' => -1, 'show' => 0];
            $list = $this->Model_urls->getList($i, $cond, (int)$sitemapConfig['404_num'], 'ASC');
            $data = "";
            foreach ($list as $item) {
                $data .= sprintf($sitemapConfig['404_url'], $item['id']) . PHP_EOL;
            }

            file_put_contents(APPPATH . sprintf($sitemapConfig['404_filePath'], $i), $data);
            $i++;
        } while (count($list) >= (int)$sitemapConfig['404_num']);
    }

    private function makeSiteMap($list, $siteMapFilePath)
    {
        $xmlText = ('<?xml version="1.0" encoding="UTF-8"?>') . PHP_EOL;
        $xmlText .= ('<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">') . PHP_EOL . PHP_EOL . PHP_EOL;

        //sitemap列表
        foreach ($list as $item) {
            $xmlText .= ('<url>') . PHP_EOL;
            $xmlText .= "  " . ('<loc>') . $item['url'] . ('</loc>') . PHP_EOL;
            $xmlText .= ('</url>') . PHP_EOL;
        }

        $xmlText .= ('</urlset>');
        file_put_contents($siteMapFilePath, $xmlText);
    }

    private function makeSiteMapIndex($siteMapUrl, $page, $siteMapIndexFilePath)
    {
        $xmlIndexText = ('<?xml version="1.0" encoding="utf-8"?>') . PHP_EOL;
        $xmlIndexText .= ('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">') . PHP_EOL;

        //sitemap列表
        for ($i = 1; $i <= $page; $i++) {
            $xmlIndexText .= ('<sitemap>') . PHP_EOL;
            $xmlIndexText .= ('<loc>') . sprintf($siteMapUrl, $i) . ('</loc>') . PHP_EOL;
            $xmlIndexText .= ('<lastmod>') . date('Y-m-d', time()) . ('</lastmod>') . PHP_EOL;
            $xmlIndexText .= ('</sitemap>') . PHP_EOL;
        }

        $xmlIndexText .= ('</sitemapindex>');
        file_put_contents($siteMapIndexFilePath, $xmlIndexText);
    }
}
